<template>
  <div id="globalHeader">
    <a-row class="grid-demo" align="center" :wrap="false">
      <a-col flex="auto">
        <a-menu
          mode="horizontal"
          :selected-keys="selectKeys"
          @menu-item-click="handleMenuItemClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img
                class="logo"
                src="https://img1.baidu.com/it/u=434181393,2232767281&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
              />
              <span class="title">QJOJ</span>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in visibleRoutes" :key="item.path"
            >{{ item.name }}
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="100px">
        <div>{{ store.state.user?.loginUser?.userName ?? "未登录" }}</div>
      </a-col>
    </a-row>
  </div>
</template>
<script setup lang="ts">
import routes from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";

const router = useRouter();
const store = useStore();

const visibleRoutes = computed(() =>
  routes.filter((item) => {
    if (item.meta?.hiddenInMenu) {
      return false;
    }
    if (
      !checkAccess(store.state.user?.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  })
);
const selectKeys = ref([router.currentRoute.value.path]);
router.afterEach((to, from, next) => {
  selectKeys.value = [to.path];
});
const handleMenuItemClick = (key: string) => {
  router.push({ path: key });
};
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title-bar .logo {
  width: 48px;
}

.title-bar .title {
  color: black;
  font-size: large;
  margin-left: 20px;
}
</style>
